#1.3寸 TFT 显示屏 (ST7789 驱动芯片)驱动程序
"""
ST7789 TFT显示屏驱动模块

该模块提供1.3寸TFT显示屏(ST7789)的基本驱动功能,包括:
- 显示屏初始化
- 显示图像
- 清屏
- 显示文字
- 基本图形绘制
"""

from machine import Pin, SPI
import time

class ST7789Display:
    def __init__(self, spi_bus=1, dc_pin=2, cs_pin=5, rst_pin=15, bl_pin=13):
        """初始化显示屏
        
        参数:
            spi_bus: SPI总线号
            dc_pin: DC引脚号
            cs_pin: CS引脚号  
            rst_pin: 复位引脚号
            bl_pin: 背光引脚号
        """
        # 初始化SPI
        self.spi = SPI(spi_bus, baudrate=40000000, polarity=0, phase=0)
        
        # 初始化控制引脚
        self.dc = Pin(dc_pin, Pin.OUT)
        self.cs = Pin(cs_pin, Pin.OUT)
        self.rst = Pin(rst_pin, Pin.OUT)
        self.bl = Pin(bl_pin, Pin.OUT)
        
        # 显示屏参数
        self.width = 240
        self.height = 240
        
        # 初始化显示屏
        self._init_display()
        
    def _init_display(self):
        """显示屏硬件初始化"""
        # 复位显示屏
        self.rst.value(1)
        time.sleep_ms(5)
        self.rst.value(0)
        time.sleep_ms(20)
        self.rst.value(1)
        time.sleep_ms(150)
        
        # 发送初始化命令序列
        self._send_command(0x11)  # Sleep out
        time.sleep_ms(120)
        
        self._send_command(0x36)  # Memory data access control
        self._send_data(0x00)
        
        self._send_command(0x3A)  # Interface pixel format
        self._send_data(0x05)     # 16-bit color
        
        # 开启显示
        self._send_command(0x29)  # Display on
        
        # 打开背光
        self.bl.value(1)
        
    def _send_command(self, cmd):
        """发送命令"""
        self.dc.value(0)
        self.cs.value(0)
        self.spi.write(bytes([cmd]))
        self.cs.value(1)
        
    def _send_data(self, data):
        """发送数据"""
        self.dc.value(1)
        self.cs.value(0)
        self.spi.write(bytes([data]))
        self.cs.value(1)
        
    def clear(self, color=0x0000):
        """清屏
        
        参数:
            color: 16位RGB颜色值
        """
        # 设置显示区域
        self._send_command(0x2A)  # Column address set
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0xEF)
        
        self._send_command(0x2B)  # Row address set
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0xEF)
        
        # 填充颜色
        self._send_command(0x2C)  # Memory write
        self.dc.value(1)
        self.cs.value(0)
        for i in range(self.width * self.height):
            self.spi.write(bytes([color >> 8, color & 0xFF]))
        self.cs.value(1)
        
    def display_image(self, image_data):
        """显示图像
        
        参数:
            image_data: 图像数据,格式为RGB565
        """
        # 设置显示区域
        self._send_command(0x2A)
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0xEF)
        
        self._send_command(0x2B)
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0x00)
        self._send_data(0xEF)
        
        # 写入图像数据
        self._send_command(0x2C)
        self.dc.value(1)
        self.cs.value(0)
        self.spi.write(image_data)
        self.cs.value(1)
